
def locate(path_list, to_find, info)
	# replace started *. or * in pattern to a valid regex
	if to_find.start_with?("*.")
		to_find = to_find.gsub(/^\*\./, ".*\\.")
	elsif to_find.start_with?("*")
		to_find = "." + to_find
	end
	
	if info
		puts "search files like `#{to_find}`"
		puts "------------------------------"
	end
	
	found = 0
	path_list.each do |path|
		if path.length != 0
			if File.exist?(path) and File.directory?(path)
				dir = Dir.new(path)
				dir.each do |file|
					if file.match(to_find) != nil
						puts dir.path + "\\" + file
						found = found + 1
					end
				end
			end
		end
	end
	
	if info
		if found > 0
			puts ""
		end
		puts "#{found} file(s) found."
	end
end

def main
	path = '.;' + ENV['PATH']
	dirs = path.split(';').uniq()
	if ARGV.size > 0
		if ARGV[0] == "-i"
			locate dirs, ARGV[1], true
		else
			locate dirs, ARGV[0], false
		end
	else
		puts "locate - search files match to pattern in $PATH (Environment Variable)"
		puts "usage: locate [-i] PATTERN"
		puts "       -i -- show extra information"
		puts "  PATTERN -- files to find in regex pattern"
	end
end

main